package com.hb.trade.api.routine.controller.project.notice;

import cn.hutool.core.util.StrUtil;
import com.hb.core.pojo.center.au.entity.User;
import com.hb.core.pojo.enums.InviteStatusEnum;
import com.hb.core.pojo.enums.NewsTypeEnum;
import com.hb.core.pojo.enums.NewsTypeTwoEnum;
import com.hb.core.pojo.parent.PageResult;
import com.hb.core.pojo.project.notice.dto.NoticeDTO;
import com.hb.core.pojo.project.notice.dto.TenderInviteEnterpriseDTO;
import com.hb.trade.api.routine.util.SendNewsUtil;
import com.hb.trade.client.center.UserClient;
import com.hb.trade.client.project.TenderInviteEnterpriseClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.fzzn.core.auth.shiro.util.ShiroUtil;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.msg.ResponseDto;
import org.fzzn.core.util.Enums;
import org.fzzn.core.util.ResultUtil;
import org.fzzn.core.web.anno.Version;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 邀请书的操作
 *
 * @author tian
 * @date 2022年02月19日 16:28
 */
@Slf4j
@Version(1.0)
@Api(tags = {"邀请书的操作"})
@RequestMapping("/{version}")
@RestController
public class ApiTenderInviteEnterpriseController {

    protected final TenderInviteEnterpriseClient tenderInviteEnterpriseClient;
    protected final UserClient userClient;

    public ApiTenderInviteEnterpriseController(TenderInviteEnterpriseClient tenderInviteEnterpriseClient,
                                               UserClient userClient) {
        this.tenderInviteEnterpriseClient = tenderInviteEnterpriseClient;
        this.userClient = userClient;
    }


    /**
     * 查询投标人邀请书列表信息
     *
     * @param tenderInviteEnterpriseDTO
     * @return org.fzzn.core.model.msg.ResponseDto
     * @author tian
     * @date 2022-2-19 17:05
     */
    @ApiOperation(value = "查询投标人邀请书列表信息", notes = "查询投标人邀请书列表信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectName", value = "项目名称", dataTypeClass = TenderInviteEnterpriseDTO.class, required = false),
    })
    @PostMapping("/tenderInviteEnterprise/findTenderInviteEnterpriseList")
    public ResponseDto findTenderInviteEnterpriseList(@RequestBody TenderInviteEnterpriseDTO tenderInviteEnterpriseDTO) {

        // 企业id
        tenderInviteEnterpriseDTO.setOrgId(ShiroUtil.getOrgId().toString());

        Message<PageResult<TenderInviteEnterpriseDTO>> noticeList = tenderInviteEnterpriseClient.findTenderInviteEnterpriseList(tenderInviteEnterpriseDTO);
        return ResultUtil.msg(noticeList);
    }


    /**
     * 根据id查询邀请书详情
     *
     * @param tenderInviteEnterpriseDTO
     * @return org.fzzn.core.model.msg.ResponseDto
     * @author tian
     * @date 2022-2-19 17:05
     */
    @ApiOperation(value = "根据id查询邀请书详情", notes = "根据id查询邀请书详情", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "邀请书Id", dataTypeClass = TenderInviteEnterpriseDTO.class),
    })
    @PostMapping("/tenderInviteEnterprise/findTenderInviteEnterpriseInfoById")
    public ResponseDto findTenderInviteEnterpriseInfoById(@RequestBody TenderInviteEnterpriseDTO tenderInviteEnterpriseDTO) {
        // 基本参数验证
        String addParamsValidMsg = tenderInviteEnterpriseDTO.paramsValid();
        if (StrUtil.isNotEmpty(addParamsValidMsg)) {
            return ResultUtil.fail(addParamsValidMsg);
        }

        Message<TenderInviteEnterpriseDTO> tenderInviteEnterprise = tenderInviteEnterpriseClient.findTenderInviteEnterpriseInfoById(tenderInviteEnterpriseDTO);

        // 如果联系人 是空的就直接去企业信息查询
        if (StringUtils.isEmpty(tenderInviteEnterprise.getData().getContacts())) {
            // 查询企业联系人 联系方式
            Message<User> user = userClient.getByUserId(ShiroUtil.getUserId());
            //企业id
            if (user.isSuccess() && user.getData() != null) {
                tenderInviteEnterprise.getData().setContacts(user.getData().getFullName());
                tenderInviteEnterprise.getData().setContactsPhone(user.getData().getPhone());
            }
        }
        return ResultUtil.msg(tenderInviteEnterprise);
    }



    /**
     * 投标人确认回执
     *
     * @param tenderInviteEnterpriseDTO
     * @return org.fzzn.core.model.msg.ResponseDto
     * @author tian
     * @date 2022-2-19 17:05
     */
    @ApiOperation(value = "投标人确认回执", notes = "投标人确认回执", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "邀请书Id", dataTypeClass = TenderInviteEnterpriseDTO.class),
            @ApiImplicitParam(name = "contacts", value = "联系人", dataTypeClass = TenderInviteEnterpriseDTO.class, required = false),
            @ApiImplicitParam(name = "contactsPhone", value = "联系方式", dataTypeClass = TenderInviteEnterpriseDTO.class, required = false),
            @ApiImplicitParam(name = "inviteStatus", value = "邀请状态( 2 已接收 3 已拒绝)", dataTypeClass = TenderInviteEnterpriseDTO.class),
            @ApiImplicitParam(name = "receiptPdf", value = "回执文件", dataTypeClass = TenderInviteEnterpriseDTO.class, required = false),
            @ApiImplicitParam(name = "reasonForRejection", value = "拒绝原因", dataTypeClass = TenderInviteEnterpriseDTO.class, required = false),
    })
    @PostMapping("/tenderInviteEnterprise/confirmationReceipt")
    public ResponseDto confirmationReceipt(@RequestBody TenderInviteEnterpriseDTO tenderInviteEnterpriseDTO) {
        // 基本参数验证
        String addParamsValidMsg = tenderInviteEnterpriseDTO.paramsValid();
        if (StrUtil.isNotEmpty(addParamsValidMsg)) {
            return ResultUtil.fail(addParamsValidMsg);
        }
        // 记录接受人员id
        tenderInviteEnterpriseDTO.setReceiptId(ShiroUtil.getUserId().toString());
        Message<Boolean> booleanMessage = tenderInviteEnterpriseClient.confirmationReceipt(tenderInviteEnterpriseDTO);
        //给招标人发送消息
        if (Boolean.TRUE.equals(booleanMessage.getData())){
            Message<TenderInviteEnterpriseDTO> inviteEnterpriseInfoBySectionId = tenderInviteEnterpriseClient.findInviteEnterpriseInfoById(tenderInviteEnterpriseDTO);
            if (inviteEnterpriseInfoBySectionId.getData() == null){
                return ResultUtil.msg(booleanMessage);
            }

            TenderInviteEnterpriseDTO data = inviteEnterpriseInfoBySectionId.getData();
            //给招标人发送审核消息
            SendNewsUtil.sendTendereeNews(
                    data.getSectionId(),
                    NewsTypeEnum.FILING_REVIEW,
                    NewsTypeTwoEnum.TENDEREE_NOTICE_AUDIT,
                    ShiroUtil.getUsername(),
                    Enums.nameOf(InviteStatusEnum.class,tenderInviteEnterpriseDTO.getInviteStatus()),
                    "/console/bidInviteReceipt",
                    null
            );
        }
        return ResultUtil.msg(booleanMessage);
    }


    /**
     * 代理机构查询每个标段下邀请回执查看
     * @author tian
     * @date 2022-2-21 14:11
     * @param tenderInviteEnterpriseDTO
     * @return org.fzzn.core.model.msg.ResponseDto
     */
    @ApiOperation(value = "代理机构查询每个标段下邀请回执查看", notes = "代理机构查询每个标段下邀请回执查看", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目Id", dataTypeClass = TenderInviteEnterpriseDTO.class),
    })
    @PostMapping("/tenderInviteEnterprise/findInviteEnterpriseListByProjectId")
    public ResponseDto findInviteEnterpriseListByProjectId(@RequestBody TenderInviteEnterpriseDTO tenderInviteEnterpriseDTO) {
        // 基本参数验证
        String addParamsValidMsg = tenderInviteEnterpriseDTO.paramsValid();
        if (StrUtil.isNotEmpty(addParamsValidMsg)) {
            return ResultUtil.fail(addParamsValidMsg);
        }

        Message<PageResult<TenderInviteEnterpriseDTO>> tenderInviteEnterpriseList = tenderInviteEnterpriseClient.findInviteEnterpriseListByProjectId(tenderInviteEnterpriseDTO);
        return ResultUtil.msg(tenderInviteEnterpriseList);
    }

    /**
     * 代理机构根据标段id查询当前标段下投标人邀请信息
     * @author tian
     * @date 2022-2-21 16:54
     * @param tenderInviteEnterpriseDTO
     * @return org.fzzn.core.model.msg.ResponseDto
     */
    @ApiOperation(value = "代理机构根据标段id查询当前标段下投标人邀请信息", notes = "代理机构根据标段id查询当前标段下投标人邀请信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段Id", dataTypeClass = TenderInviteEnterpriseDTO.class),
    })
    @PostMapping("/tenderInviteEnterprise/findInviteEnterpriseInfoBySectionId")
    public ResponseDto findInviteEnterpriseInfoBySectionId(@RequestBody TenderInviteEnterpriseDTO tenderInviteEnterpriseDTO) {
        // 基本参数验证
        String addParamsValidMsg = tenderInviteEnterpriseDTO.paramsValid();
        if (StrUtil.isNotEmpty(addParamsValidMsg)) {
            return ResultUtil.fail(addParamsValidMsg);
        }

        Message<TenderInviteEnterpriseDTO> inviteEnterprise = tenderInviteEnterpriseClient.findInviteEnterpriseInfoBySectionId(tenderInviteEnterpriseDTO);
        return ResultUtil.msg(inviteEnterprise);
    }



    /**
     * 代理机构根据标段id查询当前标段下投标人邀请信息
     * @author tian
     * @date 2022-2-21 16:54
     * @param tenderInviteEnterpriseDTO
     * @return org.fzzn.core.model.msg.ResponseDto
     */
    @ApiOperation(value = "代理机构根据标段id查询当前标段下投标人邀请信息", notes = "代理机构根据标段id查询当前标段下投标人邀请信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段Id", dataTypeClass = TenderInviteEnterpriseDTO.class),
    })
    @PostMapping("/tenderInviteEnterprise/findInviteEnterprisePageBySectionId")
    public ResponseDto findInviteEnterprisePageBySectionId(@RequestBody TenderInviteEnterpriseDTO tenderInviteEnterpriseDTO) {
        // 基本参数验证
        String addParamsValidMsg = tenderInviteEnterpriseDTO.paramsValid();
        if (StrUtil.isNotEmpty(addParamsValidMsg)) {
            return ResultUtil.fail(addParamsValidMsg);
        }

        Message<PageResult<TenderInviteEnterpriseDTO>> inviteEnterprise = tenderInviteEnterpriseClient.findInviteEnterprisePageBySectionId(tenderInviteEnterpriseDTO);
        return ResultUtil.msg(inviteEnterprise);
    }
}
